<template>
  <div class="page-wrap">
    <el-card shadow="never">
      <div class="toolbar">
        <el-input v-model="filters.search" placeholder="搜索门店名称/编码/城市" clearable class="w-300" />
        <el-button type="primary" @click="openEdit()">新增门店</el-button>
        <el-button @click="exportCsv">导出</el-button>
      </div>
      <el-table :data="dataList" border stripe>
        <el-table-column prop="name" label="门店名称" min-width="160" />
        <el-table-column prop="code" label="编码" width="120" />
        <el-table-column prop="city" label="城市" width="120" />
        <el-table-column prop="address" label="地址" min-width="220" />
        <el-table-column label="营业时间" width="160">
          <template #default="{ row }">{{ row.openTime }} - {{ row.closeTime }}</template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100" />
        <el-table-column label="收银点数量" width="120">
          <template #default="{ row }">{{ getRegistersByStore(row.id).length }}</template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button size="small" @click="openEdit(row)">编辑</el-button>
            <el-button size="small" @click="openRegisters(row)">收银点</el-button>
            <el-button size="small" type="danger" @click="onDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 门店编辑 -->
    <el-dialog v-model="visible" :title="form.id?'编辑门店':'新增门店'" width="680px">
      <el-form :model="form" label-width="100px">
        <el-form-item label="门店名称"><el-input v-model="form.name" /></el-form-item>
        <el-form-item label="编码"><el-input v-model="form.code" /></el-form-item>
        <el-form-item label="城市"><el-input v-model="form.city" /></el-form-item>
        <el-form-item label="地址"><el-input v-model="form.address" /></el-form-item>
        <el-form-item label="营业时间">
          <div class="time-range">
            <el-time-select v-model="form.openTime" start="06:00" step="00:30" end="23:30" placeholder="开门" />
            <span> - </span>
            <el-time-select v-model="form.closeTime" start="06:00" step="00:30" end="23:30" placeholder="打烊" />
          </div>
        </el-form-item>
        <el-form-item label="状态"><el-select v-model="form.status"><el-option label="营业" value="open"/><el-option label="停业" value="closed"/></el-select></el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="visible=false">取消</el-button>
        <el-button type="primary" @click="onSave">保存</el-button>
      </template>
    </el-dialog>

    <!-- 收银点管理 -->
    <el-dialog v-model="regVisible" :title="`收银点 - ${currentStore?.name||''}`" width="720px">
      <div class="toolbar">
        <el-button type="primary" @click="openRegEdit()">新增收银点</el-button>
      </div>
      <el-table :data="registerList" border stripe>
        <el-table-column prop="name" label="名称" min-width="160" />
        <el-table-column prop="sn" label="终端SN" width="160" />
        <el-table-column prop="channel" label="渠道" width="120" />
        <el-table-column prop="status" label="状态" width="100" />
        <el-table-column label="操作" width="160" fixed="right">
          <template #default="{ row }">
            <el-button size="small" @click="openRegEdit(row)">编辑</el-button>
            <el-button size="small" type="danger" @click="onRegDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <template #footer>
        <el-button @click="regVisible=false">关闭</el-button>
      </template>
    </el-dialog>

    <!-- 收银点编辑 -->
    <el-dialog v-model="regEditVisible" :title="regForm.id?'编辑收银点':'新增收银点'" width="520px">
      <el-form :model="regForm" label-width="100px">
        <el-form-item label="名称"><el-input v-model="regForm.name" /></el-form-item>
        <el-form-item label="终端SN"><el-input v-model="regForm.sn" /></el-form-item>
        <el-form-item label="渠道"><el-select v-model="regForm.channel"><el-option label="线下" value="线下"/><el-option label="小程序" value="小程序"/><el-option label="三方平台" value="三方平台"/></el-select></el-form-item>
        <el-form-item label="状态"><el-select v-model="regForm.status"><el-option label="启用" value="enabled"/><el-option label="停用" value="disabled"/></el-select></el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="regEditVisible=false">取消</el-button>
        <el-button type="primary" @click="onRegSave">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { computed, onMounted, reactive, ref } from 'vue'
import { useStoreStore } from '@/stores/store.js'
const store = useStoreStore()
const filters = reactive({ search: '' })
const visible = ref(false)
const regVisible = ref(false)
const regEditVisible = ref(false)
const form = reactive({ id:'', name:'', code:'', city:'', address:'', openTime:'10:00', closeTime:'22:00', status:'open' })
const currentStore = ref(null)
const regForm = reactive({ id:'', name:'', sn:'', channel:'线下', status:'enabled' })

onMounted(()=>{ store.initialize() })

const dataList = computed(()=>{
  const s = (filters.search||'').toLowerCase()
  return store.stores.filter(x=>
    !s || x.name.toLowerCase().includes(s) || (x.code||'').toLowerCase().includes(s) || (x.city||'').toLowerCase().includes(s)
  )
})
const registerList = computed(()=> currentStore.value ? store.getRegistersByStore(currentStore.value.id) : [])
const getRegistersByStore = (id)=> store.getRegistersByStore(id)

function openEdit(row){ Object.assign(form, row || { id:'', name:'', code:'', city:'', address:'', openTime:'10:00', closeTime:'22:00', status:'open' }); visible.value=true }
function onSave(){ if(form.id) store.updateStore(form.id, form); else store.addStore(form); visible.value=false }
function onDelete(row){ store.removeStore(row.id) }
function openRegisters(row){ currentStore.value=row; regVisible.value=true }

function openRegEdit(row){ Object.assign(regForm, row || { id:'', name:'', sn:'', channel:'线下', status:'enabled', storeId: currentStore.value.id }); regEditVisible.value=true }
function onRegSave(){ if(regForm.id) store.updateRegister(regForm.id, regForm); else store.addRegister(regForm); regEditVisible.value=false }
function onRegDelete(row){ store.removeRegister(row.id) }

function exportCsv(){
  const headers = ['名称','编码','城市','地址','开门','打烊','状态','收银点数']
  const rows = store.stores.map(s=>[s.name,s.code,s.city,s.address,s.openTime,s.closeTime,s.status,store.getRegistersByStore(s.id).length])
  const csv = [headers.join(','), ...rows.map(r=>r.join(','))].join('\n')
  const blob = new Blob(['\ufeff' + csv], { type: 'text/csv;charset=utf-8;' })
  const link = document.createElement('a')
  link.href = URL.createObjectURL(blob)
  link.download = `门店_${new Date().toISOString().slice(0,10)}.csv`
  link.click()
}
</script>

<style scoped>
.page-wrap { padding: 16px; }
.toolbar { display: flex; gap: 12px; margin-bottom: 12px; }
.w-300 { width: 300px; }
.time-range { display: flex; align-items: center; gap: 6px; }
</style>


